package com.vgooo.client.vchannel.album;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.vgooo.bean.Picture;
import com.vgooo.client.R;
import com.vgooo.util.AsyncImageLoader;
import com.vgooo.util.BitmapUtils;
import com.vgooo.util.DebugTools;
import com.vgooo.util.ImageLoaderTask;

public class PictureAdapter extends BaseAdapter {
	private static final String className = "PictureAdapter";
	private List<Picture> pictures;
	private LayoutInflater inflater;
	private Context context ;

	public PictureAdapter(List<Picture> pictures , Context context ) {
		super();
		setPictures(pictures);
		this.context = context ;
		inflater = LayoutInflater.from(context);
	}

	public void setPictures(List<Picture> pictures) {
		if (pictures != null && !pictures.isEmpty()) {
			this.pictures = pictures;
		}else{
			pictures = new ArrayList<Picture>() ;
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return pictures.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return pictures.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if(convertView == null ){
			convertView = inflater.inflate(R.layout.album_detail_item, null);
			holder = new ViewHolder() ;
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.image = (ImageView) convertView.findViewById(R.id.album_item_image);
		
		Picture p = pictures.get(position);
		/**
		 * 大图和小图的图片名相同？？？？
		 * 已解决
		 */
		BitmapUtils.loaderImage(p.image_small_thumbnail, holder.image,context);
		
		return convertView;
	}
	
	class ViewHolder{
		ImageView image ;
	}
	
//	/**
//	 * 在imageView上显示图片
//	 * 
//	 * @param urlPath
//	 *            图片的网络路径
//	 * @param imageView
//	 *            显示图片的控件
//	 * @param bm
//	 */
//
//	public void loaderImage(String urlPath, ImageView imageView) {
//		boolean flag = BitmapUtils.setBitmap(urlPath, imageView);
//		if (!flag) {
//			DebugTools.log(className, "首次加载:" + urlPath + "图片，从网络下载");
//			ImageLoaderTask task = new ImageLoaderTask(urlPath, imageView , context);
//			AsyncImageLoader loder = new AsyncImageLoader();
//			loder.execute(task);
//		} else {
//			DebugTools.log(className, "从缓存中加载:" + urlPath + "图片");
//		}
//	}


}
